const path = require('path');
const fs = require("fs").promises;
const ffmpeg = require('fluent-ffmpeg');

let list = [{
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1734104057424_295093892.mp4",
		"dirPath": "/videos/1734104057424_295093892.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/1734104057424_295093892_thumbnail.webp",
		"uniqueID": "ff7YU13g",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1734104057424_295093892/index.m3u8"
	}, {
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1734104097539_910838282.mp4",
		"dirPath": "/videos/1734104097539_910838282.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/1734104097539_910838282_thumbnail.webp",
		"uniqueID": "2mb393mf",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1734104097539_910838282/index.m3u8"
	}, {
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1734104129718_190239855.mp4",
		"dirPath": "/videos/1734104129718_190239855.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/1734104129718_190239855_thumbnail.webp",
		"uniqueID": "D0EYrDJp",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1734104129718_190239855/index.m3u8"
	}, {
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1734104204788_851686837.mp4",
		"dirPath": "/videos/1734104204788_851686837.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/1734104204788_851686837_thumbnail.webp",
		"uniqueID": "9IcHO5YH",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1734104204788_851686837/index.m3u8"
	}, {
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1734104253998_555157947.mp4",
		"dirPath": "/videos/1734104253998_555157947.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/1734104253998_555157947_thumbnail.webp",
		"uniqueID": "Sq5I75Pg",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1734104253998_555157947/index.m3u8"
	}, {
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1734104293928_255222569.mp4",
		"dirPath": "/videos/1734104293928_255222569.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/1734104293928_255222569_thumbnail.webp",
		"uniqueID": "dWMC0I22",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1734104293928_255222569/index.m3u8"
	}, {
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1734104314483_984977627.mp4",
		"dirPath": "/videos/1734104314483_984977627.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/1734104314483_984977627_thumbnail.webp",
		"uniqueID": "C7llsllb",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1734104314483_984977627/index.m3u8"
	}, {
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1734104335478_737402678.mp4",
		"dirPath": "/videos/1734104335478_737402678.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/1734104335478_737402678_thumbnail.webp",
		"uniqueID": "foHDx5oL",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1734104335478_737402678/index.m3u8"
	}, {
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1734104380651_133597420.mp4",
		"dirPath": "/videos/1734104380651_133597420.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/1734104380651_133597420_thumbnail.webp",
		"uniqueID": "LrE4L0c4",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1734104380651_133597420/index.m3u8"
	}, {
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1734104398512_807401337.mp4",
		"dirPath": "/videos/1734104398512_807401337.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/1734104398512_807401337_thumbnail.webp",
		"uniqueID": "oywwlbgw",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1734104398512_807401337/index.m3u8"
	}, {
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1734104437572_271196921.mp4",
		"dirPath": "/videos/1734104437572_271196921.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/1734104437572_271196921_thumbnail.webp",
		"uniqueID": "N1bPbYCe",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1734104437572_271196921/index.m3u8"
	}, {
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1734104543754_727415823.mp4",
		"dirPath": "/videos/1734104543754_727415823.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/1734104543754_727415823_thumbnail.webp",
		"uniqueID": "ogmoVBrh",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1734104543754_727415823/index.m3u8"
	}, {
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1736737702469_589838690.mp4",
		"dirPath": "/videos/1736737702469_589838690.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/1736737702469_589838690_thumbnail.webp",
		"uniqueID": "FFxXd3wH",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1736737702469_589838690/index.m3u8"
	},
	{
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1736919347736_246209571.mp4",
		"dirPath": "/videos/1736919347736_246209571.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/vt_1736919347736_246209571.webp",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1736919347736_246209571/index.m3u8",
		"uniqueID": "XciaLtij"
	}, {
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1736947270872_210916476.mp4",
		"dirPath": "/videos/1736947270872_210916476.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/vt_1736947270872_210916476.webp",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1736947270872_210916476/index.m3u8",
		"uniqueID": "l3b44u8x"
	}, {
		"type": "video/mp4",
		"path": "https://imageserve.ctrlikun.com/videos/1736925479077_837577220.mp4",
		"dirPath": "/videos/1736925479077_837577220.mp4",
		"thumbnail": "https://imageserve.ctrlikun.com/videos_thumbnail/vt_1736925479077_837577220.webp",
		"m3u8path": "https://imageserve.ctrlikun.com/videos_hls/1736925479077_837577220/index.m3u8",
		"uniqueID": "pqKTeeqT"
	}
]

console.log(list.length);
// handled()

async function handled() {
	for (let i = 0; i < list.length; i++) {
		let filename = list[i].dirPath.split('/videos/')[1]
		let orsrc = filename.split(".")[0]
		await handledCompressAndSlice(filename, orsrc);
	}
	console.log("全部执行完毕");
}

/**处理单个视频文件的压缩和切片
 *@param {*} filename 文件名称及后缀
 *@param {*} orsrc 文件名称
 */
async function handledCompressAndSlice(filename, orsrc) {
	let videoInputStream, videoOutputFile, inputStream, outputFile;
	try {
		videoInputStream = "public/videos/" + filename;
		videoOutputFile = "public/video_compress/" + filename;
		inputStream = videoInputStream;
		outputFile = `public/videos_hls/${orsrc}/index.m3u8`;
		await fs.access(videoInputStream, fs.constants.F_OK);
		// 检查并创建输出文件夹
		await fs.mkdir(path.dirname(videoOutputFile), {
			recursive: true
		});
		await fs.mkdir(`public/videos_hls/${orsrc}`, {
			recursive: true
		});
		// 压缩视频
		await new Promise((resolve, reject) => {
			ffmpeg()
				.input(videoInputStream)
				.videoCodec('libx264')
				.outputOptions('-b:v 600k')
				.outputOptions('-s 720x1280')
				.output(videoOutputFile)
				.on('error', reject)
				.on('end', async () => {
					//移动位置 替换
					await fs.rename(videoOutputFile, videoInputStream, (err) => {
						if (err) {
							inputStream = videoOutputFile;
						}
					});
					resolve()
				})
				.run();
		});

		// 切片视频为 HLS
		await new Promise((resolve, reject) => {
			ffmpeg()
				.input(inputStream)
				.videoCodec('libx264')
				.outputOptions([
					'-hls_time 5',
					'-hls_list_size 0',
					'-f hls'
				])
				.output(outputFile)
				.on('error', reject)
				.on('end', () => {
					console.log("切片处理完毕", outputFile);
					resolve();
				})
				.run();
		});

	} catch (err) {
		if (err.code === 'ENOENT') {
			console.error(`文件 ${videoInputStream} 不存在，跳过压缩和切片。`);
		} else {
			console.error('Error during compression, slicing, or file access:', err);
		}
	}
}
